package com.patrrr.Note;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.patrrr.SQLite3.DBHandler;
import com.patrrr.SQLite3.DBHelper;
import com.patrrr.Widget.NoteWidget;
import com.patrrr.app.Background;
import com.patrrr.app.NoteInfo;
import com.patrrr.util.Backuper;
import com.patrrr.util.Configuration;

public class NoteList extends Activity {
	private ListView mListView;
	private BaseAdapter mAdapter;
	private boolean isCheckedShow;
	private boolean isListLock;
	private boolean isMoveIn;
	private String[] folderNames;
	private ListView folderChooserList;

	// 当前显示状态
	private int mode = MODE_DEFAULT; // 模式变量 。
	private int folderId = DEFAULTFOLDER_ID; // 当前文件夹ID。默认为-1
	private static final int MODE_DEFAULT = 0; // 默认模式。显示所有文件夹和处于默认文件夹内的便签
	private static final int MODE_FOLDER = 1; // 文件夹模式。显示选中文件夹内的便签

	private ArrayList<Bean> mList; // 文件夹信息链表+默认文件夹内的便签链表

	private ArrayList<NoteBean> noteList; // 保存便签信息
	private ArrayList<FolderBean> folderList; // 保存所有文件夹的信息

	private ArrayList<Boolean> folderCheckStateList; // 保存文件夹上CheckBox 的状态
	private ArrayList<Boolean> noteCheckStateList; // 保存便签上CheckBox 的状态

	private TextView top_name;
	private ImageView top_add;

	private DBHandler dbHandler;

	// menu：删除
	private LinearLayout menu_delete; // 布局
	private Button menu_delete_ok; // 确定
	private Button menu_delete_cancel; // 取消

	// menu：新建文件夹
	private LinearLayout menu_newFolder; // 布局
	private TextView menu_modifyFolder_tv; //
	private EditText menu_newFolder_edit; // 输入文件名
	private Button menu_newFolder_ok; // 确定
	private Button menu_newFolder_cancel; // 取消

	// menu:移动
	private LinearLayout menu_move; // 布局
	private Button menu_move_ok; // 移进/移出文件夹
	private Button menu_move_cancel; // 取消

	public static final int DEFAULTFOLDER_ID = -1;
	private static final int DIALOG_PASSWORD = 0;
	private static final int DIALOG_RESTORE = 1;
	private static final int DIALOG_FOLDER_CHOOSER = 2;
	private static final int DIALOG_BACKUP = 3;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.noteslist);
		mListView = (ListView) findViewById(R.id.noteslist_foldersList);

		top_name = (TextView) findViewById(R.id.noteslist_top_name);
		top_add = (ImageView) findViewById(R.id.noteslist_top_tianjia);
		top_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NoteBean noteBean = new NoteBean();
				Configuration c = new Configuration(NoteList.this);
				noteBean
						.setCreateTime(Calendar.getInstance().getTimeInMillis());
				noteBean.setFolderId(folderId);
				noteBean.setColor(c.getInt(Configuration.COLOR, 0));
				noteBean.setWidgetId(0);
				goToEditor(NoteEditor.MODE_CREATE_NOTE, noteBean);
			}
		});

		mList = new ArrayList<Bean>();
		folderList = new ArrayList<FolderBean>();
		noteList = new ArrayList<NoteBean>();
		folderCheckStateList = new ArrayList<Boolean>();
		noteCheckStateList = new ArrayList<Boolean>();

		dbHandler = new DBHandler(this);
		mAdapter = new FolderListAdapter();
		
		new Backuper(this).restore();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mListView.setAdapter(mAdapter);
		if (getMode() == MODE_DEFAULT) {
			changeToDefault();
		} else {
			changeToFolder(folderId);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbHandler.close();
	}

	// 合并文件夹链表和默认文件夹内的便签链表
	private ArrayList<Bean> mergeList(ArrayList<FolderBean> folderList,
			ArrayList<NoteBean> noteList) {
		ArrayList<Bean> mList = new ArrayList<Bean>();
		if (folderList != null) {
			mList.addAll(folderList);
		}
		if (noteList != null) {
			mList.addAll(noteList);
		}
		return mList;
	}

	/**
	 * 刷新列表。
	 */
	private void refresh() {
		switch (getMode()) {
		case MODE_DEFAULT:
			folderList.clear();
			noteList.clear();
			mList.clear();
			folderList = dbHandler.getFolderList();
			noteList = dbHandler.getNoteInFolder(DEFAULTFOLDER_ID);
			mList = mergeList(folderList, noteList);
			mAdapter.notifyDataSetChanged();
			break;
		case MODE_FOLDER:
			noteList.clear();
			mList.clear();
			noteList = dbHandler.getNoteInFolder(folderId);
			mList = mergeList(null, noteList);
			mAdapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.notelist_menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		switch (getMode()) {
		case MODE_DEFAULT:
			menu.setGroupVisible(R.id.menu_notes, true);
			menu.setGroupVisible(R.id.menu_folder, false);
			break;
		case MODE_FOLDER:
			menu.setGroupVisible(R.id.menu_notes, false);
			menu.setGroupVisible(R.id.menu_folder, true);
			break;
		default:
			break;
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.notesList_menu_newNote:// 新建便签
			NoteBean noteBean0 = new NoteBean();
			Configuration c = new Configuration(this);
			noteBean0.setCreateTime(Calendar.getInstance().getTimeInMillis());
			noteBean0.setFolderId(folderId);
			noteBean0.setColor(c.getInt(Configuration.COLOR, 0));
			noteBean0.setWidgetId(0);
			goToEditor(NoteEditor.MODE_CREATE_NOTE, noteBean0);
			break;
		case R.id.notesList_menu_newFolder:// 新建文件夹
			showMenu_modifyFolder();
			break;
		case R.id.notesList_menu_moveIn:// 移进文件夹
			showMenu_move();
			break;
		case R.id.notesList_menu_delete:// 删除
			showMenu_delete();
			break;
		case R.id.notesList_menu_output:// 输出txt文本
			Backuper backuper=new Backuper(this);
			backuper.export();
			break;
		case R.id.notesList_menu_more:// 更多
			break;
		case R.id.notesList_menu_more_backup:// 更多-备份数据
			showDialog(DIALOG_BACKUP);
			break;
		case R.id.notesList_menu_more_restore:// 更多-从SD卡还原
			showDialog(DIALOG_RESTORE);
			break;
		case R.id.notesList_menu_more_setpassword:// 更多-设置密码
			showDialog(DIALOG_PASSWORD);
			break;
		case R.id.notesList_menu_more_recommend:// 更多-更多推荐
			break;
		case R.id.notesList_menu_more_about:// 更多-关于
			break;
		// ///////////////////////////////////////////////
		// //////////// Folder /////////////////////
		// /////////////////////////////////////////
		case R.id.folder_menu_newNote:// 新建便签
			NoteBean noteBean1 = new NoteBean();
			Configuration c1 = new Configuration(this);
			noteBean1.setCreateTime(Calendar.getInstance().getTimeInMillis());
			noteBean1.setFolderId(folderId);
			noteBean1.setColor(c1.getInt(Configuration.COLOR, 0));
			noteBean1.setWidgetId(0);
			goToEditor(NoteEditor.MODE_CREATE_NOTE, noteBean1);
			break;
		case R.id.folder_menu_modify:// 修改文件夹////////////////////////////////////////修改文件夹
			showMenu_modifyFolder();
			break;
		case R.id.folder_menu_moveOut:// 移出文件夹
			showMenu_move();
			break;
		case R.id.folder_menu_delete:// 删除
			showMenu_delete();
			break;
		case R.id.folder_menu_desktop:// 添加到桌面
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 显示删除窗口。同时锁定ListView使不可点击
	 */
	private void showMenu_delete() {
		if (menu_delete == null) {
			menu_delete = (LinearLayout) findViewById(R.id.notelist_delete);
			menu_delete_ok = (Button) menu_delete
					.findViewById(R.id.notelist_delete_Ok);
			menu_delete_cancel = (Button) menu_delete
					.findViewById(R.id.notelist_delete_cancel);

			menu_delete_ok.setOnClickListener(deleteListener);
			menu_delete_cancel.setOnClickListener(deleteListener);
		}
		// 这里会导致当前Activity所有界面重绘？
		// set Visible之后无需手动对ListView实现重绘
		// 显示前重置所有checkBox状态为未选中
		resetFolderCheckedState(folderList.size(), false);
		resetNoteCheckState(noteList.size(), false);
		setCheckBoxVisible(true);
		menu_delete.setVisibility(View.VISIBLE);
		lockList();
	}

	/**
	 * 显示修改文件夹窗口，同时锁定ListView使不可点击
	 */
	private void showMenu_modifyFolder() {
		if (menu_newFolder == null) {
			menu_newFolder = (LinearLayout) findViewById(R.id.notelist_newFolder);
			menu_modifyFolder_tv = (TextView) menu_newFolder
					.findViewById(R.id.noteList_modifyFolder_tv);
			menu_newFolder_edit = (EditText) menu_newFolder
					.findViewById(R.id.noteList_newFolder_edit);
			menu_newFolder_ok = (Button) menu_newFolder
					.findViewById(R.id.noteList_newFOlder_ok);
			menu_newFolder_cancel = (Button) menu_newFolder
					.findViewById(R.id.noteList_newFOlder_cancel);

			menu_newFolder_ok.setOnClickListener(modifyFolderListener);
			menu_newFolder_cancel.setOnClickListener(modifyFolderListener);
		}
		if (getMode() == MODE_DEFAULT) {
			menu_modifyFolder_tv.setText("新建文件夹");
		} else {
			menu_modifyFolder_tv.setText("修改文件夹");
		}
		menu_newFolder.setVisibility(View.VISIBLE);
		lockList();
	}

	/**
	 * 显示移动窗口。同时锁定ListView使不可点击
	 */
	private void showMenu_move() {
		if (menu_move == null) {
			menu_move = (LinearLayout) findViewById(R.id.notelist_move);
			menu_move_ok = (Button) menu_move
					.findViewById(R.id.notelist_move_Ok);
			menu_move_cancel = (Button) menu_move
					.findViewById(R.id.notelist_move_cancel);

			menu_move_ok.setOnClickListener(moveListener);
			menu_move_cancel.setOnClickListener(moveListener);
		}
		if (isMoveIn) {
			menu_move_ok.setText(getString(R.string.moveIn));
		} else {
			menu_move_ok.setText(getString(R.string.moveOut));
		}
		resetFolderCheckedState(folderList.size(), false);
		resetNoteCheckState(noteList.size(), false);
		setCheckBoxVisible(true);
		menu_move.setVisibility(View.VISIBLE);
		lockList();
	}

	/**
	 * 关闭删除窗口。同时解锁ListView使可点击
	 */
	private void dimissMenu_delete() {
		if (menu_delete == null) {
			return;
		}
		setCheckBoxVisible(false);
		// 这里会导致当前Activity所有界面重绘？
		// set GONE之后无需手动对ListView实现重绘
		// ///////////////////////////////////////////////
		menu_delete.setVisibility(View.GONE);
		unlockList();
	}

	/**
	 * 关闭修改文件夹窗口。同时解锁ListView使可点击
	 */
	private void dimissMenu_newFolder() {
		if (menu_newFolder == null) {
			return;
		}
		menu_newFolder_edit.setText("");
		menu_newFolder.setVisibility(View.GONE);
		unlockList();
	}

	/**
	 * 关闭移动窗口。同时解锁ListView使可点击
	 */
	private void dimissMenu_Move() {
		if (menu_move == null) {
			return;
		}
		setCheckBoxVisible(false);
		menu_move.setVisibility(View.GONE);
		unlockList();
	}

	private OnClickListener deleteListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.notelist_delete_Ok:
				for (int i = 0; i < folderCheckStateList.size(); i++) {
					if (folderCheckStateList.get(i)) {
						deleteFolder(folderList.get(i).getId());
					}
				}
				for (int i = 0; i < noteCheckStateList.size(); i++) {
					if (noteCheckStateList.get(i)) {
						deleteNote(noteList.get(i).getId());
						if (noteList.get(i).getWidgetId() != 0) {
							Intent intent = new Intent(NoteList.this,
									NoteList.class);
							PendingIntent pendingIntent = PendingIntent
									.getActivity(NoteList.this, 0, intent,
											PendingIntent.FLAG_UPDATE_CURRENT);
							RemoteViews views = new RemoteViews(NoteList.this
									.getPackageName(), R.layout.widget);
							views
									.setTextViewText(
											R.id.widget_text,
											NoteList.this
													.getString(R.string.widget_contentHasBeenDeleted));
							views.setOnClickPendingIntent(R.id.widget_layout,
									pendingIntent);
							AppWidgetManager.getInstance(NoteList.this)
									.updateAppWidget(
											noteList.get(i).getWidgetId(),
											views);
						}
					}
				}
				dimissMenu_delete();
				refresh();
				break;
			case R.id.notelist_delete_cancel:
				dimissMenu_delete();
				break;
			default:
				break;
			}
		}
	};

	private OnClickListener modifyFolderListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.noteList_newFOlder_ok:
				String newFolderName = menu_newFolder_edit.getText().toString();
				if (newFolderName.equals("") || newFolderName == null) {
					break;
				}
				ContentValues contentValues = new ContentValues();
				contentValues.put(DBHelper.FOLDER_NAME, newFolderName);
				if (getMode() == MODE_DEFAULT) {
					dbHandler.insert(DBHelper.FOLDER, contentValues);
				} else {
					dbHandler.update(DBHelper.FOLDER, contentValues, folderId);
				}
				dimissMenu_newFolder();
				refresh();
				break;
			case R.id.noteList_newFOlder_cancel:
				dimissMenu_newFolder();
				break;
			default:
				break;
			}
		}
	};
	private OnClickListener moveListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.notelist_move_Ok:
				if (isMoveIn) {
					refreshFolderNames();
					new AlertDialog.Builder(NoteList.this).setItems(
							folderNames, chooserListener).show();
				} else {
					for (int i = 0; i < noteCheckStateList.size(); i++) {
						if (noteCheckStateList.get(i)) {
							moveOut(noteList.get(i).getId());
						}
					}
					refresh();
				}
				dimissMenu_Move();
				break;
			case R.id.notelist_move_cancel:
				dimissMenu_Move();
				break;
			default:
				break;
			}
		}
	};
	private DialogInterface.OnClickListener chooserListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			int fId = folderList.get(which).getId();
			for (int i = 0; i < noteCheckStateList.size(); i++) {
				if (noteCheckStateList.get(i)) {
					moveInto(noteList.get(i).getId(), fId);
				}
			}
			refresh();
		}

	};
	private DialogInterface.OnClickListener backupListener=new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			if(which==0){
				Backuper backuper=new Backuper(NoteList.this);
				backuper.backup();
			}else if(which==1){
				
			}
		}
	};

	private void refreshFolderNames() {
		folderNames = new String[folderList.size()];
		for (int i = 0; i < folderList.size(); i++) {
			folderNames[i] = folderList.get(i).getFolderName();
		}
	}

	/**
	 * 重置folderCheckStateList内所有值
	 */
	private void resetFolderCheckedState(int listSize, boolean b) {
		folderCheckStateList.clear();
		for (int i = 0; i < listSize; i++) {
			folderCheckStateList.add(i, b);
		}
	}

	/**
	 * 重置noteCheckStateList内所有值
	 * 
	 * @param listSize
	 * @param b
	 */
	private void resetNoteCheckState(int listSize, boolean b) {
		noteCheckStateList.clear();
		for (int i = 0; i < listSize; i++) {
			noteCheckStateList.add(i, b);
		}
	}

	/**
	 * 删除DBHelper_FOLDER表中_id为folderId的文件夹。若此文件夹内含有便签，一并删除
	 * 
	 * @param folderId
	 */
	private void deleteFolder(int folderId) {
		dbHandler.delete(DBHelper.FOLDER, folderId);// 删除文件夹表内数据
		// 若此文件夹内存在便签，删除
		dbHandler.delete(folderId);
	}

	/**
	 * 删除DBHelper_Note表中_id为id为id的便签
	 * 
	 * @param id
	 */
	private void deleteNote(int noteId) {
		dbHandler.delete(DBHelper.NOTE, noteId);
	}

	/**
	 * 将DBHelper_Note表中_id为id的便签所属的文件夹更改为默认文件夹
	 * 
	 * @param noteId
	 */
	private void moveOut(int noteId) {
		ContentValues values = new ContentValues();
		values.put(DBHelper.NOTE_FOLDERID, DEFAULTFOLDER_ID);
		dbHandler.update(DBHelper.NOTE, values, noteId);
	}

	/**
	 * 将DBHelper_Note表中_id为noteId的便签的所属文件夹id更改为folderId
	 * 
	 * @param folderId
	 */
	private void moveInto(int noteId, int folderId) {
		ContentValues values = new ContentValues();
		values.put(DBHelper.NOTE_FOLDERID, folderId);
		dbHandler.update(DBHelper.NOTE, values, noteId);
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		// TODO Auto-generated method stub
		switch (id) {
		case DIALOG_RESTORE:// 还原
			return new AlertDialog.Builder(this).setMessage(
					R.string.restore_warn).setPositiveButton(R.string.OK, null)
					.setNegativeButton(R.string.Cancel, null).create();
		case DIALOG_PASSWORD:// 设置密码
			View password_view = getLayoutInflater().inflate(
					R.layout.set_password, null);
			return new AlertDialog.Builder(this).setTitle(R.string.setPassword)
					.setView(password_view)
					.setPositiveButton(R.string.OK, null).setNegativeButton(
							R.string.Cancel, null).create();
		case DIALOG_FOLDER_CHOOSER: // 文件夹列表选择
			return new AlertDialog.Builder(this).setView(folderChooserList)
					.create();
		case DIALOG_BACKUP:
			return new AlertDialog.Builder(this).setItems(R.array.backup, backupListener)
					.create();
		default:
			return super.onCreateDialog(id, args);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK: // 拦截 返回键
			if (menu_delete != null && menu_delete.isShown()) {
				dimissMenu_delete();
				return true;
			}
			if (menu_newFolder != null && menu_newFolder.isShown()) {
				dimissMenu_newFolder();
				return true;
			}
			if (menu_move != null && menu_move.isShown()) {
				dimissMenu_Move();
				return true;
			}
			if (getMode() == MODE_FOLDER) {
				changeToDefault();
				return true;
			}
			break;
		case KeyEvent.KEYCODE_MENU: // 拦截 menu 键
			if (menu_newFolder != null && menu_newFolder.isShown()) {
				return true;
			}
			if (menu_delete != null && menu_delete.isShown()) {
				return true;
			}
			if (menu_move != null && menu_move.isShown()) {
				return true;
			}
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 转入 NoteEditor
	 * 
	 * @param action
	 * @param noteBean
	 */
	private void goToEditor(String action, NoteBean noteBean) {
		Intent i = new Intent(this, NoteEditor.class);
		i.setAction(action);
		i.putExtra(NoteInfo.NOTE_BEAN, noteBean);
		startActivity(i);
	}

	/**
	 * 转换为默认状态。显示所有文件夹和默认文件夹内的便签
	 */
	private void changeToDefault() {
		folderList.clear();
		noteList.clear();
		folderList = dbHandler.getFolderList();
		noteList = dbHandler.getNoteInFolder(DEFAULTFOLDER_ID);
		mList.clear();
		mList = mergeList(folderList, noteList);
		mAdapter.notifyDataSetChanged();
		setMode(MODE_DEFAULT);
		setIsMoveIn(true);
		setFolderId(DEFAULTFOLDER_ID);
		setTitleName(getString(R.string.MyNote));
	}

	/**
	 * 转换为文件夹状态。显示DBHelper_Note内folderId为folderId的便签
	 * 
	 * @param folderId
	 */
	private void changeToFolder(int folderId) {
		folderList.clear();
		noteList.clear();
		noteList = dbHandler.getNoteInFolder(folderId);
		mList.clear();
		mList = mergeList(null, noteList);
		mAdapter.notifyDataSetChanged();
		setMode(MODE_FOLDER);
		setIsMoveIn(false);
		setFolderId(folderId);
		DBHandler dbHandler = new DBHandler(this);
		setTitleName(dbHandler.getFolderName(folderId));
		dbHandler.close();
	}

	private String getDateStr(long timeInMills) {
		Date date = new Date(timeInMills);
		Date curDate = new Date();
		if (curDate.getTime() - timeInMills < 24 * 60 * 60 * 1000) {
			return new SimpleDateFormat("kk:mm").format(date);
		} else {
			return new SimpleDateFormat("EEEE").format(date);
		}
	}

	private void setCheckBoxVisible(boolean b) {
		this.isCheckedShow = b;
	}

	private void lockList() {
		this.isListLock = true;
	}

	private void unlockList() {
		this.isListLock = false;
	}

	private void setIsMoveIn(boolean b) {
		this.isMoveIn = b;
	}

	private void setMode(int i) {
		this.mode = i;
	}

	private int getMode() {
		return this.mode;
	}

	private void setFolderId(int folerId) {
		this.folderId = folerId;
	}

	private int getFolderId() {
		return this.folderId;
	}

	private void setTitleName(String name) {
		top_name.setText(name);
	}

	class FolderHolder {
		TextView folderName;
		TextView amount;
		TextView modifyTime;
		CheckBox checked;
	}

	class NoteHolder {
		ImageView alert;
		TextView content;
		TextView createTime;
		CheckBox checked;
		ImageView touying;
		LinearLayout bg;
	}

	private class FolderListAdapter extends BaseAdapter {

		public FolderListAdapter() {
			super();
			// TODO Auto-generated constructor stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			FolderHolder folderHolder = null;
			NoteHolder noteHolder = null;
			if (mList.get(position) instanceof FolderBean) {
				folderHolder = new FolderHolder();
				final FolderBean folderBean = (FolderBean) mList.get(position);
				convertView = getLayoutInflater().inflate(R.layout.folderitem,
						null);
				if (!isListLock) {
					convertView.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							changeToFolder(folderBean.getId());
						}
					});
				}
				folderHolder.folderName = (TextView) convertView
						.findViewById(R.id.folderItem_folderName);
				folderHolder.modifyTime = (TextView) convertView
						.findViewById(R.id.folderItem_modifyTime);
				folderHolder.checked = (CheckBox) convertView
						.findViewById(R.id.folderItem_checked);

				folderHolder.folderName.setText(folderBean.getFolderName());
				if (isCheckedShow) {
					folderHolder.checked.setVisibility(View.VISIBLE);
					folderHolder.checked.setChecked(folderCheckStateList
							.get(position));
				} else {
					folderHolder.checked.setVisibility(View.GONE);
				}
				final int i = position;
				folderHolder.checked.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (((CheckBox) v).isChecked()) {
							folderCheckStateList.set(i, true);
						} else {
							folderCheckStateList.set(i, false);
						}
					}
				});
			}

			if (mList.get(position) instanceof NoteBean) {
				final int i = position - folderList.size();
				noteHolder = new NoteHolder();
				final NoteBean noteBean = (NoteBean) mList.get(position);
				convertView = getLayoutInflater().inflate(R.layout.noteitem,
						null);
				noteHolder.bg = (LinearLayout) convertView
						.findViewById(R.id.noteItem_bg_linearLayout);
				if (!isListLock) {
					noteHolder.bg.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							goToEditor(NoteEditor.MODE_VIEW_NOTE, noteBean);
						}
					});
				}
				noteHolder.alert = (ImageView) convertView
						.findViewById(R.id.noteItem_alarm);
				noteHolder.content = (TextView) convertView
						.findViewById(R.id.noteItem_content);
				noteHolder.createTime = (TextView) convertView
						.findViewById(R.id.noteItem_createTime);
				noteHolder.checked = (CheckBox) convertView
						.findViewById(R.id.noteItem_checked);
				noteHolder.touying = (ImageView) convertView
						.findViewById(R.id.noteItem_touying);

				if (noteBean.getAlertTime() > 0) {
					noteHolder.alert.setVisibility(View.VISIBLE);
				} else {
					noteHolder.alert.setVisibility(View.GONE);
				}
				noteHolder.bg.setBackgroundResource(Background.item_bg[noteBean
						.getColor()]);
				noteHolder.content.setText(noteBean.getContent());
				noteHolder.createTime.setText(getDateStr(noteBean
						.getCreateTime()));

				if (isCheckedShow) {
					noteHolder.checked.setVisibility(View.VISIBLE);
					noteHolder.checked.setChecked(noteCheckStateList.get(i));
				} else {
					noteHolder.checked.setVisibility(View.GONE);
				}
				noteHolder.checked.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (((CheckBox) v).isChecked()) {
							noteCheckStateList.set(i, true);
						} else {
							noteCheckStateList.set(i, false);
						}
					}
				});
				if (position == mList.size() - 1) {
					noteHolder.touying.setVisibility(View.VISIBLE);
				} else {
					noteHolder.touying.setVisibility(View.GONE);
				}
				convertView.setPadding(0, 0, 0, 1);
			}
			return convertView;
		}
	}

}